package cl.orangepeople.whyem.portal.backend.api.impl;

import cl.orangepeople.whyem.portal.backend.api.NegotiationsService;
import cl.orangepeople.whyem.portal.backend.dto.negotiations.NegotiationDTO;
import cl.orangepeople.whyem.portal.backend.dto.negotiations.NegotiationDataBean;
import cl.orangepeople.whyem.portal.backend.dto.negotiations.NegotiationDataDetails;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NegotiationServiceImpl implements NegotiationsService {

    private static Logger logger;
    private static String[] clientsID;
    private static String[] paymentNumbers;
    private static String[] productsName;

    static {
        logger = LoggerFactory.getLogger(DebtsServiceImpl.class);
        clientsID = new String[]{"15956647", "158011638", "111111111", "15801163", "11111111"};
        paymentNumbers = new String[]{"12345678", "321654981", "321654982", "321654983", "321654984"};
        productsName = new String[]{"Cr\u00e9dito de consumo", "Cr\u00e9dito hipotecario", "Tarjeta de Cr\u00e9dito"};
    }

    public NegotiationDataBean getNegotiationDetail(NegotiationDTO negotiationData) {
        NegotiationDataBean bean = new NegotiationDataBean();
        NegotiationDataDetails details = new NegotiationDataDetails();
        String clientID = negotiationData.getClientID();
        String paymentNumber = negotiationData.getNegotiationID();
        details.setPaymentNumber(new BigDecimal("28402859028509820572974"));
        Object[] objectToBeLogged = new Object[]{clientID, paymentNumber};
        logger.debug("Client id: {}, payment number: {}", objectToBeLogged);

        boolean existsClientID = ArrayUtils.contains(clientsID, clientID);
        boolean existsPayment = ArrayUtils.contains(paymentNumbers, paymentNumber);
        boolean existsPaymebtData = existsClientID && existsPayment;
        if (existsPaymebtData) {
            bean.setReturnCode(0);
            details.setClientID(new BigDecimal("15956647"));
            details.setClientName("Paolo Mezzano");
            details.setDescription(productsName[0]);
            details.setAmount("259990");
        } else {
            bean.setReturnCode(-1);
        }

        bean.setNegotiationDetail(details);

        return bean;
    }

    public List<NegotiationDataBean> getNegotiationsByClientID(String clientID) {
        List<NegotiationDataBean> negotiations = new ArrayList<NegotiationDataBean>();
        boolean existClientID = ArrayUtils.contains(clientsID, clientID);
        if (existClientID) {
            for (int i = 1; i <= 4; i++) {
                NegotiationDataBean bean = new NegotiationDataBean();
                NegotiationDataDetails beanDetails = new NegotiationDataDetails();
                bean.setReturnCode(0);
                bean.setNegotiationDetail(beanDetails);
                beanDetails.setClientID(new BigDecimal(clientID));
                beanDetails.setClientName("Paolo Mezzano");
                beanDetails.setDescription(productsName[new Random().nextInt(productsName.length)]);
                beanDetails.setPaymentNumber(new BigDecimal(321654980 + i));
                Locale locale = new Locale("es", "CL");
                NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(locale);
                String trxAmmountFormatted = numberFormatter.format((int) ((Math.random() * 5) * 100000));
                trxAmmountFormatted = StringUtils.remove(trxAmmountFormatted, "Ch");
                beanDetails.setAmount(trxAmmountFormatted);
                negotiations.add(bean);
            }
        }
        return negotiations;
    }
}